package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Biao;
import com.hopu.pojo.Emp;
import com.hopu.pojo.Mybs;
import com.hopu.pojo.Shouru;
import com.hopu.service.BsService;
import com.hopu.service.ShouruService;
import com.hopu.util.DateFormatGetTime;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/shouru")
@RestController
public class ShouruController {
    @Resource
    private ShouruService shouruServiceImpl;

    @PostMapping("/findAllShouru")
    private ResultUtils findAllShouru(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Shouru> allShouru = shouruServiceImpl.findAllShouru();

            PageInfo pageInfo = new PageInfo(allShouru);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("addShouru")
    public ResultUtils addShouru(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "smoney")Float smoney,
            @RequestParam(value = "stm")String stm,
            @RequestParam(value = "sremark")String sremark

    ){
        try {
            Emp empLog = (Emp) request.getSession().getAttribute("empLog");

            int num = shouruServiceImpl.addShouru(empLog.getEid(),smoney,stm,new Date().getTime(),sremark);

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/findShouruBySid")
    public ResultUtils findShouruBySid(int sid){

        Shouru shouru = shouruServiceImpl.findShouruBySid(sid);

        return new ResultUtils(200,"查询页面成功",shouru);

    }
    @PostMapping("uppShouru")
    public ResultUtils uppShouru(Shouru shouru, HttpSession session){

            // 获取session 当前登录账号信息
            Emp empLog = (Emp)session.getAttribute("empLog");
            shouru.setEid(empLog.getEid());
            long time = new Date().getTime();
            shouru.setStime(time);
        int biao1 = shouruServiceImpl.uppShouru(shouru);
            if (biao1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);


    }
}
